<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2017/12/5
 * Time: 10:33
 */

namespace app\admin\controller;

use app\admin\model\SiteInfo as SiteInfoModel;
use think\Request;


class SiteinfoController extends BaseController
{
    public function lst()
    {
        $data = SiteInfoModel::order('create_time', 'asc')->paginate(6);
        $page = $data->render();
        $this->assign([
            'page' => $page,
            'data' => $data,
        ]);
        return $this->fetch();
    }

    public function add()
    {
        $request = Request::instance();
        switch  ($request->method()){
            case "GET":
                return $this->fetch();
                break;
            case "POST":
                $params = $request->param();
                $controller = $this->getLowerController();
                if(request()->file('pic')){
                    $picInfo = uploadOne($controller,'thumb','pic',80,60);
                    $params['pic'] = $picInfo['pic'];
                    $params['sm_pic'] = $picInfo['thumb_pic'];
                }
                $model = new SiteInfoModel();
                if($params['status'] == 1){
                    $model->where('status','eq',1)->setField('status',2);
                }
                if($model->validate('SiteInfoValidate')->allowField(true)->save($params)){
                    $this->success('添加站点信息!',url('admin/siteinfo/lst'),'',1);
                    exit;
                }
                else{
                    $this->error($model->getError());
                    exit;
                }
                break;
            default:
                return "错误的数据信息";
        }
    }

    public function edi()
    {
        $request = Request::instance();
        switch  ($request->method()){
            case "GET":
                $id = $request->param('id');
                $this->assign('data',SiteInfoModel::find($id));
                return $this->fetch();
                break;
            case "POST":
                $params = $request->param();
                $model = new SiteInfoModel();
                if(request()->file('pic')){
                    $oldPic = $model->where('id','eq',$params['id'])->find();
                    if(is_file($oldPic['pic'])){
                        unlink($oldPic['pic']);
                    }
                    if(is_file($oldPic['sm_pic'])){
                        unlink($oldPic['sm_pic']);
                    }
                    $controller = strtolower($request->controller());
                    $picInfo = uploadOne($controller,'thumb','pic',80,60);
                    $params['pic'] = $picInfo['pic'];
                    $params['sm_pic'] = $picInfo['thumb_pic'];
                }
                if($params['status'] == 1){
                    $model->where('status','eq',1)->setField('status',2);
                }
                if($model->validate('NodeValidate')->allowField(true)->isUpdate(true)->save($params)){
                    $this->success('修改站点信息成功!',url('admin/siteinfo/lst'),'',1);
                    exit;
                }
                else{
                    $this->error($model->getError());
                    exit;
                }
                break;
            default:
                return "错误的数据信息";
        }
    }

    public function del()
    {
        $id = $this->request->param("id");
        $dataInfo = SiteInfoModel::find($id);
        if($dataInfo->status == "应用"){
            return '当前信息正在应用中,请勿删除';
        }
        if(is_file($dataInfo->pic)){
            unlink($dataInfo->pic);
        }
        if(is_file($dataInfo->sm_pic)){
            unlink($dataInfo->sm_pic);
        }
        if(SiteInfoModel::destroy($id)){
            return '亲,您已删除成功!';
        }
        return '删除失败,发生了什么^O^';
    }
}